define([ './module' ], function(settingmodule) {
	'use strict';

	settingmodule.controller('setting',['$scope','$http','$location',function($scope,$http,$location){
		$scope.title="系统参数设置";
		
		var operate=$location.search().operate;
		if(operate!=undefined&&operate=="edit"){
			
			//默认开启保留原图
			$scope.issaveoriginalbitmap="1";
			//默认开启压缩图片
			$scope.isimagecompression="1";
			//默认关闭积分抵扣订单金额
			$scope.isusepointtodiscountorderamount="0";
			//默认关闭优惠券抵扣订单金额
			$scope.isusevoucherdiscountorderamount="0";
			//默认统一运费5元
			$scope.freight="5";
			
			$scope.updatebtn={show:true};
			$http({
				method:'POST',
				url:'../mall/setting/find.action'					
			}).
			success(function(data,status,headers,config){
				if(data.sucflag){
					angular.forEach(data.configs,function(d,i,a){
						if(i=="icp"){
							$scope.icp=d;
						}
						if(i=="issaveoriginalbitmap"){
							$scope.issaveoriginalbitmap=d;
						}
						if(i=="isimagecompression"){
							$scope.isimagecompression=d;
						}
						if(i=="defaultheight"){
							$scope.defaultheight=d;
						}
						if(i=="defaultwidth"){
							$scope.defaultwidth=d;
						}
						if(i=="thumbnailwidthlow"){
							$scope.thumbnailwidthlow=d;
						}
						if(i=="thumbnailheightlow"){
							$scope.thumbnailheightlow=d;
						}
						if(i=="thumbnailheightmiddle"){
							$scope.thumbnailheightmiddle=d;
						}
						if(i=="thumbnailwidthmiddle"){
							$scope.thumbnailwidthmiddle=d;
						}
						if(i=="thumbnailwidthhigh"){
							$scope.thumbnailwidthhigh=d;
						}
						if(i=="thumbnailheighthigh"){
							$scope.thumbnailheighthigh=d;
						}
						if(i=="freefreight"){
							$scope.freefreight=d;
						}
						if(i=="isusepointtodiscountorderamount"){
							$scope.isusepointtodiscountorderamount=d;
						}
						if(i=="integralconversionration"){
							$scope.integralconversionration=d;
						}
						if(i=="isusevoucherdiscountorderamount"){
							$scope.isusevoucherdiscountorderamount=d;
						}
						if(i=="freight"){
							$scope.freight=d;
						}
						
						
						
					});
				}
			}).
			error(function(data,status,headers,config){
				$scope.errors={
						show:true,
						msg:'系统异常'
				}
			});
		};
		
		
		$scope.update=function(){
			if($scope.icp==undefined){
				$scope.errors={
						show:true,
						msg:'请输入备案号'
				}
				return false;
			}
			if($scope.defaultheight==undefined){
				$scope.errors={
						show:true,
						msg:'请输入默认图片高度'
				}
				return false;
			}
			if($scope.defaultwidth==undefined){
				$scope.errors={
						show:true,
						msg:'请输入默认图片宽度'
				}
				return false;
			}
			if($scope.thumbnailheightlow==undefined){
				$scope.errors={
						show:true,
						msg:'请输入低质量压缩图高度'
				}
				return false;
			}
			if($scope.thumbnailwidthlow==undefined){
				$scope.errors={
						show:true,
						msg:'请输入低质量压缩图宽度'
				}
				return false;
			}
			if($scope.thumbnailheightmiddle==undefined){
				$scope.errors={
						show:true,
						msg:'请输入中等质量压缩图高度'
				}
				return false;
			}
			if($scope.thumbnailwidthmiddle==undefined){
				$scope.errors={
						show:true,
						msg:'请输入中等质量压缩图宽度'
				}
				return false;
			}
			if($scope.thumbnailheighthigh==undefined){
				$scope.errors={
						show:true,
						msg:'请输入高等质量压缩图高度'
				}
				return false;
			}
			if($scope.thumbnailwidthhigh==undefined){
				$scope.errors={
						show:true,
						msg:'请输入高等质量压缩图宽度'
				}
				return false;
			}
			if($scope.freefreight==undefined){
				$scope.errors={
						show:true,
						msg:'请输入免运费额度'
				}
				return false;
			}
			if($scope.integralconversionration==undefined){
				$scope.errors={
						show:true,
						msg:'请输入积分兑换比率1积分＝0.01元'
				}
				return false;
			}
			if($scope.freight==undefined){
				$scope.errors={
						show:true,
						msg:'请输入统一运费'
				}
				return false;
			}
			
			
			$http({
				method:'POST',
				url:'../mall/setting/update.action',
				data:{
					"icp":$scope.icp,
					"issaveoriginalbitmap":$scope.issaveoriginalbitmap,
					"isimagecompression":$scope.isimagecompression,
					"defaultheight":$scope.defaultheight,
					"defaultwidth":$scope.defaultwidth,
					"thumbnailwidthlow":$scope.thumbnailwidthlow,
					"thumbnailheightlow":$scope.thumbnailheightlow,
					"thumbnailheightmiddle":$scope.thumbnailheightmiddle,
					"thumbnailwidthmiddle":$scope.thumbnailwidthmiddle,
					"thumbnailwidthhigh":$scope.thumbnailwidthhigh,
					"thumbnailheighthigh":$scope.thumbnailheighthigh,
					"freefreight":$scope.freefreight,
					"isusepointtodiscountorderamount":$scope.isusepointtodiscountorderamount,
					"integralconversionration":$scope.integralconversionration,
					"isusevoucherdiscountorderamount":$scope.isusevoucherdiscountorderamount,
					"freight":$scope.freight
				}
			})
			.success(function(data,status,headers,config){
				if(data.sucflag){
					$scope.errors={};
					$scope.info={
							show:true,
							msg:'系统配置更新成功'
					}
					$location.path('/setting');
				}
			}).
			error(function(data,status,headers,config){
				$scope.errors={
						show:true,
						msg:'系统异常'
				}
			});
		};
		
	}]);

	
});
